package com.ruoyi.memo.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.memo.domain.MemoNote;
import com.ruoyi.memo.mapper.MemoNoteMapper;
import com.ruoyi.memo.service.IMemoNoteService;
import com.ruoyi.common.utils.DateUtils;

import java.util.List;

@Service
public class MemoNoteServiceImpl implements IMemoNoteService {

    @Autowired
    private MemoNoteMapper memoNoteMapper;

    @Override
    public MemoNote selectMemoNoteByNoteId(Long noteId) {
        return memoNoteMapper.selectMemoNoteByNoteId(noteId);
    }

    @Override
    public List<MemoNote> selectMemoNoteList(MemoNote memoNote) {
        return memoNoteMapper.selectMemoNoteList(memoNote);
    }

    @Override
    public int insertMemoNote(MemoNote memoNote) {
        //创建String数组
        String[] cnm = {"cnm", "草泥马","SB"};
        //如果内容包含 “cnm""草泥马”等就返回
        for (String s : cnm) {
            if (memoNote.getContent().contains(s)){
                return 0;
            }
        }
        //如果包含同类词就返回
        memoNote.setUserId(SecurityUtils.getUserId());
        List list =memoNoteMapper.selectMemoNoteList(memoNote);
        if (list.size()>0){
            return 0;
        }
        // 1. 校验便签对象非空
        if (memoNote == null) {
            throw new ServiceException("便签对象不能为空");
        }

        // 2. 标题非空校验
        if (memoNote.getTitle() == null || memoNote.getTitle().trim().isEmpty()) {
            throw new ServiceException("标题不能为空");
        }

        // 3. 标题长度校验（1-50字符）
        if (memoNote.getTitle().length() > 50) {
            throw new ServiceException("标题长度不能超过50字符");
        }

        // 4. 内容长度校验（0-2000字符）
        if (memoNote.getContent() != null && memoNote.getContent().length() > 2000) {
            throw new ServiceException("内容长度不能超过2000字符");
        }

        // 5. 重要性校验（只能是1、2、3）
        if (memoNote.getImportance() != null &&
                !("1".equals(memoNote.getImportance()) ||
                        "2".equals(memoNote.getImportance()) ||
                        "3".equals(memoNote.getImportance()))) {
            throw new ServiceException("重要性只能是1(最重要)、2(中等)、3(一般)");
        }

        // 6. 设置创建时间
        memoNote.setCreateTime(DateUtils.getNowDate());

        return memoNoteMapper.insertMemoNote(memoNote);
    }

    @Override
    public int updateMemoNote(MemoNote memoNote) {
        // 1. 校验便签对象非空
        if (memoNote == null) {
            throw new ServiceException("便签对象不能为空");
        }

        // 2. 便签ID非空校验
        if (memoNote.getNoteId() == null) {
            throw new ServiceException("便签ID不能为空");
        }

        // 3. 标题长度校验（1-50字符）
        if (memoNote.getTitle() != null && memoNote.getTitle().length() > 50) {
            throw new ServiceException("标题长度不能超过50字符");
        }

        // 4. 内容长度校验（0-2000字符）
        if (memoNote.getContent() != null && memoNote.getContent().length() > 2000) {
            throw new ServiceException("内容长度不能超过2000字符");
        }

        // 5. 重要性校验（只能是1、2、3）
        if (memoNote.getImportance() != null &&
                !("1".equals(memoNote.getImportance()) ||
                        "2".equals(memoNote.getImportance()) ||
                        "3".equals(memoNote.getImportance()))) {
            throw new ServiceException("重要性只能是1(最重要)、2(中等)、3(一般)");
        }

        // 6. 设置更新时间
        memoNote.setUpdateTime(DateUtils.getNowDate());

        return memoNoteMapper.updateMemoNote(memoNote);
    }

    @Override
    public int deleteMemoNoteByNoteIds(Long[] noteIds) {
        if (noteIds == null || noteIds.length == 0) {
            throw new ServiceException("请选择要删除的便签");
        }
        return memoNoteMapper.deleteMemoNoteByNoteIds(noteIds);
    }

    @Override
    public int deleteMemoNoteByNoteId(Long noteId) {
        if (noteId == null) {
            throw new ServiceException("便签ID不能为空");
        }
        return memoNoteMapper.deleteMemoNoteByNoteId(noteId);
    }
}